cssjshtml vue.js 父组件向子组件传值

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4461

2019/01/07 20:10


 

从App.vue向components/Users.vue传值

App.vue

<!-- 1模板:html结构 -->
<template>
  <div id="app">
    <app-header></app-header>
    <users v-bind:users="users"></users>
    <app-footer></app-footer>
  </div>
</template>

<!-- 2行为:逻辑处理 -->
<script>
  // 局部注册组件
  import Users from './components/Users'
  import Header from './components/Header'
  import Footer from './components/Footer'

  export default {
    name: 'App',
    data() {
      return {
        // title: "这是一个干净的脚手架项目!"
        users:[
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false},
          {name:"brownwang",position:"运维开发",show:false}]
      }
    },
    components: {
      "users": Users,
      "app-header": Header,
      "app-footer": Footer

    }
  }
</script>

<!-- 3样式:解决样式 -->
<style scoped>
  h1 {
    color: purple;
  }
</style>

Users.vue

<template>

  <div class="users">
    <h1>hello users</h1>
    <ul>
      <li v-for="user in users"
      v-on:click="user.show=!user.show">
       <h2>{{user.name}}</h2>
        <h3 v-show="user.show">{{user.position}}</h3>
      </li>
    </ul>
  </div>
</template>

<script>
  export default {
    name: "users",
 //   props:["users"],
    props:{
      users:{
        type:Array,
        required:true
      }
    },
    data() {
      return {
      }
    }
  }
</script>

<style scoped>
.users{
  width:100%;
  max-width:1200px;
  margin:40px auto;
  padding:0 20px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
  ul{
    display:flex;
    flex-wrap:wrap;
    list-style-type:none;
    padding:0;
  }
  li{
    flex-grow:1;
    flex-basis:200px;
    text-align:center;
    padding:30px;
    border:1px solid #222;
    margin:10px;
  }
</style>

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1